﻿using PSI.Helper;
using PSI.Models.TMS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace PSI.Services.TMS {
    public class FuelService : PSIBaseService {
        public List<Fuel> GetFuelListData() {
            var result = new List<Fuel>();

            var sql = "select id, f_from_weight, f_to_weight, f_fuel_count from tms_fuel order by f_from_weight";
            Query(sql, reader => {
                result.Add(new Fuel { 
                    Id = reader.GetString(0),
                    FromWeight = reader.GetInt32(1),
                    ToWeight = reader.GetInt32(2),
                    FuelCount = reader.GetInt32(3)
                });
            });
            
            return result;
        }

        public Result Edit(Fuel fuel) {
            if (string.IsNullOrEmpty(fuel.Id)) {
                // 新增油耗

                //检查里程上下限是否和现有的重合
                var sql = @"select count(*) from tms_fuel
                            where (f_from_weight <= @fromWeight and @fromWeight <= f_to_weight)
                                or (f_from_weight <= @toWeight and @toWeight <= f_to_weight)";
                var p = CreateDbParameters();
                p.AddWithValue("fromWeight", fuel.FromWeight);
                p.AddWithValue("toWeight", fuel.ToWeight);
                var cnt = QueryCount(sql, p);
                if (cnt > 0) {
                    return new Result { Success = false, Messgae = "里程上下限和现有的标准重合" };
                }

                fuel.Id = IdGeneratorService.NewId();
                sql = "insert into tms_fuel(id, f_from_weight, f_to_weight, f_fuel_count) values(@id, @fromWeight, @toWeight, @fuelCount)";
                p = CreateDbParameters();
                p.AddWithValue("id", fuel.Id);
                p.AddWithValue("fromWeight", fuel.FromWeight);
                p.AddWithValue("toWeight", fuel.ToWeight);
                p.AddWithValue("fuelCount", fuel.FuelCount);

                Execute(sql, p);
            } else {
                // 编辑油耗

                //检查里程上下限是否和现有的重合
                var sql = @"select count(*) from tms_fuel
                            where (id <> @id) and ((f_from_weight <= @fromWeight and @fromWeight <= f_to_weight)
                                or (f_from_weight <= @toWeight and @toWeight <= f_to_weight))";
                var p = CreateDbParameters();
                p.AddWithValue("id", fuel.Id);
                p.AddWithValue("fromWeight", fuel.FromWeight);
                p.AddWithValue("toWeight", fuel.ToWeight);
                var cnt = QueryCount(sql, p);
                if (cnt > 0) {
                    return new Result { Success = false, Messgae = "里程上下限和现有的标准重合" };
                }

                sql = @"update tms_fuel
                            set f_from_weight = @fromWeight, f_to_weight = @toWeight,
                                f_fuel_count = @fuelCount
                            where id = @id";
                p = CreateDbParameters();
                p.AddWithValue("id", fuel.Id);
                p.AddWithValue("fromWeight", fuel.FromWeight);
                p.AddWithValue("toWeight", fuel.ToWeight);
                p.AddWithValue("fuelCount", fuel.FuelCount);

                Execute(sql, p);
            }

            return new Result { Success = true, Id = fuel.Id };
        }

        public Result Delete(string id) {
            var sql = "delete from tms_fuel where id = @id";
            var p = CreateDbParameters();
            p.AddWithValue("id", id);
            Execute(sql, p);

            return new Result { Success = true };
        }
    }
}